home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 050a / tse_mac1.zip / COMPARE4.ZIP / COMPARE.S
Text File  |  1993-04-03  |  7KB  |  184 lines

  1. /*  COMPARE.S - Macro to compare the first 255 characters of
  2.     lines in two files and highlight the lines that are different
  3.     between them.  Allows the slave file to be changed by the
  4.     user.
  5.  
  6.     M. W. Hulse, v.1.4, April 4, 1993
  7.  
  8.     v.1.4   Find differences that are off the screen. Allow roll
  9.             left/right in lock step.  Depart master on file
  10.             <Escape>.
  11.  
  12.     v.1.3   Fixed <Escape> while looping.  Enabled linked
  13.             horizontal roll.  Cleaned up messages.
  14.  
  15.     v.1.2   Made finding line differences automatic; ie. files
  16.             roll up or down continuously until a difference is
  17.             found.  Thanks to Ray Asbury for the suggestion.
  18.  
  19.     Setup: Set the NoCompare color attribute as you wish it.
  20.  
  21.     Use:  Open the slave file with TSE.  Execute the macro.
  22.     Respond to the prompt with the name of the master file. Color
  23.     attribute will change if there is a difference between the
  24.     master and slave file cursor lines.  Cursor column stays
  25.     where you put it.
  26.  
  27.     Adjust the cursor line in the master file for differences on
  28.     line spacing with the alt cursor keys.
  29.  
  30.     Special key assignments:
  31.         <Ctrl Grey Cursor Up>         Find diff between two files vertically
  32.         <Ctrl Grey Cursor Down>                  "
  33.         <Alt Grey Cursor Right>       Move the two files in sync horizontally
  34.         <Alt Grey Cursor Left>                   "
  35.         <Alt Grey Cursor Up>          Move the master file only
  36.         <Alt Grey Cursor Down>                   "
  37.         <Escape>                      Quit
  38.  
  39.     All other keys affect the slave file only.
  40.  
  41. */
  42. INTEGER SWindow,
  43.         MWindow,
  44.         NoCompare = Color(Bright White on Red)  // Set your color scheme
  45.  
  46. STRING  MLine[255] = "",                    // To hold 1st 255 char of Master
  47.         SLine[255] = ""                     //              "          Slave
  48.  
  49. PROC Msg()
  50.     Message("L ", CurrLine(), " C ", CurrCol(), " Press <Esc> to exit...")
  51. END
  52.  
  53. STRING PROC GetALine()                      // Get 1st 255 characters of line
  54.     Return(Gettext(1,255))
  55. END
  56.  
  57. INTEGER PROC FindDiff()
  58.  
  59. INTEGER i = length(MLine),
  60.         j = length(SLine),
  61.         k,
  62.         l = 0
  63.  
  64.     k = iIF(i > j, i,j)                         // find longest line
  65.  
  66.     Repeat                                      // find start of discrepancy
  67.         l = l + 1
  68.     Until (SubStr(MLine, l, 1) <> SubStr(SLine, l, 1)) // compare lines
  69.     Return(l)
  70. END
  71.  
  72. PROC Paintit()
  73.  
  74. INTEGER l = FindDiff()
  75.  
  76.     Integer m
  77.  
  78.     If l >= Query(WindowCols)               // handle differences off to the right
  79.         GotoColumn(l + (Query(WindowCols) / 2)) // get into the right context
  80.         GotoColumn(l + 3)                   // put the cursor there
  81.         GotoWindow(MWindow)                 // same for the master window
  82.         GotoColumn(l + (Query(WindowCols) / 2))
  83.         GotoColumn(l + 3)
  84.         l = (Query(WindowCols) / 2) + 4         // set the beginning of highlight
  85.         m = l - 2                               // and the end
  86.     Else                                        // when in left margin window
  87.         GotoColumn(l)                           // put cursor there
  88.         GotoWindow(MWindow)                     // same for the master window
  89.         GotoColumn(l)
  90.         m = l - 1                               // and the end
  91.         l = l + 1                               // set the beginning of highlight
  92.     EndIf
  93.  
  94.         GotoWindow(SWindow)                     // Get in slave window
  95.         UpdateDisplay()                         // clean up
  96.         VGotoXY((l), (WhereY()))                // Got to Window line beginning
  97.         PutAttr(NoCompare, Query(WindowCols) - m) // color line
  98.         Msg()                                   // reminds user
  99. END
  100.  
  101. PROC CompareLines(INTEGER Direction)            // Compares lines in files and
  102.     INTEGER i,
  103.     Done = FALSE                                // paints those that are different
  104.  
  105.     Loop
  106.         If Direction                            // Roll both lines down and get
  107.             If RollDown()
  108.                 SLine = GetALine()              // ...1st 255 char. of line in
  109.                 GotoWindow(MWindow)
  110.                 If RollDown()
  111.                     MLine = GetALine()          // ...each file
  112.                 Else
  113.                     Done = TRUE
  114.                 EndIf
  115.                 GotoWindow(SWindow)
  116.             Else
  117.                 Done = TRUE
  118.             EndIf
  119.         Else                                    // same for going up
  120.             If RollUp()                         // roll up in Slave Window
  121.                 SLine = GetALine()              // call user PROC to get line data
  122.                 GotoWindow(MWindow)             // change windows
  123.                 If RollUp()                     // roll up in Master Window
  124.                     MLine = GetALine()
  125.                 Else
  126.                     Done = TRUE
  127.                 EndIf
  128.                 GotoWindow(SWindow)             // change windows back
  129.             Else
  130.                 Done = TRUE
  131.             EndIf
  132.         EndIf
  133.         Message("L ", CurrLine(), " C ", CurrCol())
  134.         If SLine <> MLine                       // if lines in the 2 files different
  135.             PaintIt()
  136.             Break
  137.         Else                                // next command reminds user
  138.             Msg()                           // remind user
  139.         EndIf
  140.         If Done == TRUE
  141.             Break
  142.         EndIf
  143.  
  144.         If (KeyPressed() AND GetKey() == <Escape>)
  145.             EndProcess()                    // get out while rolling
  146.         EndIf
  147.     EndLoop
  148. END
  149.  
  150. KEYDEF CKeys                                // special keys for this app
  151.     <Ctrl CursorUp>         CompareLines(0) // call PROC. 0 = UP
  152.     <Ctrl CursorDown>       CompareLines(1) //     "      1 = Down
  153.  
  154.     <Alt GreyCursorRight>   RollRight(4) GotoWindow(MWindow) RollRight(4) GotoWindow(SWindow)
  155.     <Alt GreyCursorLeft>    RollLeft(4)  GotoWindow(MWindow) RollLeft(4)  GotoWindow(SWindow)
  156.  
  157. // next 2 commands move in master
  158.     <Alt GreyCursorUp>      GotoWindow(MWindow) RollUp() GotoWindow(SWindow)
  159.     <Alt GreyCursorDown>    GotoWindow(MWindow) RollDown() GotoWindow(SWindow)
  160.     <Escape>                EndProcess()    // Done
  161. END
  162.  
  163. PROC mCompare()
  164.  
  165.     Set(Break, On)                          // debug only
  166.     SWindow = WindowID()                    // get current (slave) window id
  167.     Message("Enter master filename...")
  168.     HWindow()                               // ask user to load master file
  169.     EditFile()                              // ...into a window
  170.     UpdateDisplay(_STATUSLINE_REFRESH_)
  171.     MWindow = WindowID()                    // get that window ID
  172.     GotoWindow(SWindow)                     // go to slave window
  173.  
  174.     Enable(CKeys)                           // Make special keys effective
  175.     Process()                               // and act on them
  176.     Disable(CKeys)                          // quit when done
  177.     GotoWindow(MWindow)
  178.     AbandonFile()                           // Leave the master file
  179.     CloseWindow()
  180.     UpdateDisplay(_All_Windows_Refresh_)    // cleanup display
  181.     Message("Exited Compare...")
  182. END
  183.  
  184. <Alt Q><C> mCompare()